package JPetStore.web.servlet;

import JPetStore.domain.Cart;
import JPetStore.domain.Item;
import JPetStore.domain.Order;
import JPetStore.persistence.ItemDao;
import JPetStore.persistence.impl.ItemDaoImpl;
import JPetStore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

public class FinalOrderJQueryServlet extends HttpServlet {
    private OrderService orderService;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try{
            this.orderService=new OrderService();
            int orderId=this.orderService.getLatestOrderId();
            Order order=this.orderService.getOrder(orderId);
            HttpSession session=req.getSession();
            Cart cart=(Cart)session.getAttribute("cart");


            // 构建 HTML 字符串，回显接收到的数据
            String html =
                     "<div class=\"table-container\">\n"
                    + "    <table>\n"
                    + "        <tr><th colspan=\"5\">Order#"+order.getOrderDate()+"</th></tr>\n"
                    + "        <tr><th colspan=\"5\">Payment details</th></tr>\n"
                    + "        <tr><td>Card Type:</td><td colspan=\"4\">"+order.getCardType()+"</td></tr>\n"
                    + "        <tr><td>Card Number:</td><td colspan=\"4\">"+order.getCreditCard()+"</td></tr>\n"
                    + "        <tr><td>Expiry Date(MM/YYYY):</td><td colspan=\"4\">"+order.getExpiryDate()+"</td></tr>\n"

                    + "        <tr><th colspan=\"5\">Billing Address</th></tr>\n"
                    + "        <tr><td>First name:</td><td colspan=\"4\">"+order.getBillToFirstName()+"</td></tr>\n"
                    + "        <tr><td>Last name:</td><td colspan=\"4\">"+order.getBillToLastName()+"</td></tr>\n"
                    + "        <tr><td>Address1:</td><td colspan=\"4\">"+order.getBillAddress1()+"</td></tr>\n"
                    + "        <tr><td>Address2:</td><td colspan=\"4\">"+order.getBillAddress2()+"</td></tr>\n"
                    + "        <tr><td>City:</td><td colspan=\"4\">"+order.getBillCity()+"</td></tr>\n"
                    + "        <tr><td>State:</td><td colspan=\"4\">"+order.getBillState()+"</td></tr>\n"
                    + "        <tr><td>Zip:</td><td colspan=\"4\">"+order.getBillZip()+"</td></tr>\n"
                    + "        <tr><td>Country:</td><td colspan=\"4\">"+order.getBillCountry()+"</td></tr>\n"

                    + "        <tr><th colspan=\"5\">Shipping Address</th></tr>\n"
                    + "        <tr><td>First name:</td><td colspan=\"4\">"+order.getShipToFirstName()+"</td></tr>\n"
                    + "        <tr><td>Last name:</td><td colspan=\"4\">"+order.getShipToLastName()+"</td></tr>\n"
                    + "        <tr><td>Address1:</td><td colspan=\"4\">"+order.getShipAddress1()+"</td></tr>\n"
                    + "        <tr><td>Address2:</td><td colspan=\"4\">"+order.getShipAddress2()+"</td></tr>\n"
                    + "        <tr><td>City:</td><td colspan=\"4\">"+order.getShipCity()+"</td></tr>\n"
                    + "        <tr><td>State:</td><td colspan=\"4\">"+order.getShipState()+"</td></tr>\n"
                    + "        <tr><td>Zip:</td><td colspan=\"4\">"+order.getShipZip()+"</td></tr>\n"
                    + "        <tr><td>Country:</td><td colspan=\"4\">"+order.getShipCountry()+"</td></tr>\n"

                    + "        <tr><th colspan=\"5\">Status:"+order.getLineItems().get(0).getItem().getStatus()+"</th></tr>\n"
                    + "        <tr><th>ItemId</th><th>Description</th><th>Quantity</th><th>Price</th><th>Total Cost</th></tr>\n";

            ItemDao itemDao=new ItemDaoImpl();
            for(int i=0;i<order.getLineItems().size();i++){
                Item item=itemDao.getItem(order.getLineItems().get(i).getItemId());
                int quantity=order.getLineItems().get(i).getQuantity();
                 html=html+" <tr><td>"+item.getItemId()+"</td><td>"+item.getAttribute1()+"</td><td>"+quantity+"</td><td>"+ item.getListPrice()+"</td><td>"+ BigDecimal.valueOf(quantity).multiply(item.getListPrice())+"</td></tr>\n";
            }

            html=html+"        <tr><th colspan=\"5\">Total:"+order.getTotalPrice()+"</th></tr>\n"
                    + "    </table>\n"
                    + "</div>";


            // 设置响应内容类型为 HTML
            resp.setContentType("text/html;charset=UTF-8");
            PrintWriter out = resp.getWriter();
            // 输出 HTML 响应
            out.println(html);
            out.flush(); // 确保内容被发送
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
